package org.example.rowing.controller.util;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.example.rowing.enums.MedalTypeEnum;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MedalTypeEnumHandler extends BaseTypeHandler<MedalTypeEnum> {
    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, MedalTypeEnum parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter.getDisplayName());
    }

    @Override
    public MedalTypeEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String displayName = rs.getString(columnName);
        return displayName == null ? null : MedalTypeEnum.fromDisplayName(displayName);
    }

    @Override
    public MedalTypeEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String displayName = rs.getString(columnIndex);
        return displayName == null ? null : MedalTypeEnum.fromDisplayName(displayName);
    }

    @Override
    public MedalTypeEnum getNullableResult(java.sql.CallableStatement cs, int columnIndex) throws SQLException {
        String displayName = cs.getString(columnIndex);
        return displayName == null ? null : MedalTypeEnum.fromDisplayName(displayName);
    }
}
